//
//  DivideViewController.m
//  iOS UI Sample Code
//
//  Created by 규성 이 on 12. 6. 22..
//  Copyright (c) 2012년 kkamci77@gmail.com. All rights reserved.
//

#import "DivideViewController.h"

@interface DivideViewController ()

@end

@implementation DivideViewController
@synthesize bTouchMe;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)bTouchMeActionEvent:(id)sender
{
    float divideUpLine = 120.0f;
    float divedeDownLine = divideUpLine + 5.0f;
    float moveToUpDistance = 100.0f;
    float moveToDownDistance = 280.0f;
    
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); 
#if 0
    UIImageView * captureImageView = [[UIImageView alloc] initWithImage:myImage];
    [self.view addSubview:captureImageView];
    [captureImageView release];
#else
    
    UIBezierPath * myPath = [[UIBezierPath alloc] init];
    myPath.lineWidth = 1.0f;
    [myPath moveToPoint:CGPointMake(0.0f, 0.0f)];
    [myPath addLineToPoint:CGPointMake(0.0f, divedeDownLine)];
    
    for (int count=1; count<=64; count++) {
        if(count%2 == 1)
            [myPath addLineToPoint:CGPointMake(5.0f*count, divideUpLine)];
        else
            [myPath addLineToPoint:CGPointMake(5.0f*count, divedeDownLine)];
    }
    
//    [myPath addLineToPoint:CGPointMake(320.0f, 100.0f)];
    [myPath addLineToPoint:CGPointMake(320.0f, 0.0f)];
    [myPath addLineToPoint:CGPointMake(0.0f, 0.0f)];
    
    UIImage * upSideImage = [self maskImage:myImage toAreaInsidePath:myPath];
    UIImage * downSideImage = [self maskImage:myImage toAreaOutsidePath:myPath];
    
    UIImageView * ivUpSideView = [[UIImageView alloc] initWithImage:upSideImage];
    [self.view addSubview:ivUpSideView];
//    [ivUpSideView setFrame:CGRectMake(0.0f, 0.0f, upSideImage.size.width, upSideImage.size.height)];
    
    
    NSLog(@"upSideImage width=%f, height=%f", upSideImage.size.width, upSideImage.size.height);
    NSLog(@"downSideImage width=%f, height=%f", upSideImage.size.width, upSideImage.size.height);
    
    UIImageView * ivDownSideView = [[UIImageView alloc] initWithImage:downSideImage];
    [self.view addSubview:ivDownSideView];
    
//    [ivDownSideView setFrame:CGRectMake(0.0f, 100.0f, 320.0f, 360.0f)];
    
    
    NSLog(@"ivUpView x=%f, y=%f, width=%f, height=%f", ivUpSideView.frame.origin.x, ivUpSideView.frame.origin.y, ivUpSideView.frame.size.width, ivUpSideView.frame.size.height);
    NSLog(@"ivDownView x=%f, y=%f, width=%f, height=%f", ivDownSideView.frame.origin.x, ivDownSideView.frame.origin.y, ivDownSideView.frame.size.width, ivDownSideView.frame.size.height);
    [myPath release];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, divideUpLine, 320.0f, 400.0f)];
    [testView setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
    [self.view insertSubview:testView belowSubview:ivUpSideView];
    
    
    [UIView beginAnimations:nil context:nil];
    [ivUpSideView setFrame:CGRectMake(ivUpSideView.frame.origin.x, ivUpSideView.frame.origin.y - moveToUpDistance, ivUpSideView.frame.size.width, ivUpSideView.frame.size.height)];
    [ivDownSideView setFrame:CGRectMake(ivDownSideView.frame.origin.x, ivDownSideView.frame.origin.y + moveToDownDistance, ivDownSideView.frame.size.width, ivDownSideView.frame.size.height)];
    [testView setFrame:CGRectMake(testView.frame.origin.x, testView.frame.origin.y - moveToUpDistance, testView.frame.size.width, testView.frame.size.height)];
    [UIView commitAnimations];
    
    [ivUpSideView release];
    [ivDownSideView release];
    [testView release];
    
#endif    
}

- (UIImage*) compositeImage:(UIImage*) sourceImage onPath:(UIBezierPath*) path usingBlendMode:(CGBlendMode) blend
{
    // Create a new image of the same size as the source.
    UIGraphicsBeginImageContext([sourceImage size]);
    
    // First draw an opaque path...
    [path fill];
    // ...then composite with the image.
    [sourceImage drawAtPoint:CGPointZero blendMode:blend alpha:1.0];
    
    // With drawing complete, store the composited image for later use.
    UIImage *maskedImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // Graphics contexts must be ended manually.
    UIGraphicsEndImageContext();
    
    return maskedImage;
}

- (UIImage*) maskImage:(UIImage*) sourceImage toAreaInsidePath:(UIBezierPath*) maskPath
{
    return [self compositeImage:sourceImage onPath:maskPath usingBlendMode:kCGBlendModeSourceIn];
}

- (UIImage*) maskImage:(UIImage*) sourceImage toAreaOutsidePath:(UIBezierPath*) maskPath
{
    return [self compositeImage:sourceImage onPath:maskPath usingBlendMode:kCGBlendModeSourceOut];
}

@end
